package com.org.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.org.model.User;
import com.org.service.impl.UserServiceImpl;
import com.org.util.Status;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.print.attribute.standard.MediaSize;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author test
 * @since 2021-06-05
 */
@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserServiceImpl serviceImp;

    private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";

    @PostMapping("/getUserData")
    public Map<String,Object> getUserData(){
        Map<String,Object> data = new HashMap<>();
        List<User> maps = serviceImp.selectAll();
        data.put("code",200);
        data.put("msg","success");
        data.put("data",maps);
        return data;
    }

    @PostMapping("/changePassword")
    public Map<String,Object> changePassword(@RequestParam(name = "uid") Integer uid,
                                             @RequestParam(name = "oldpassword") String oldpassword,
                                             @RequestParam(name = "newpassword") String newpassword){
        Map<String,Object> data = new HashMap<>();
        User user = serviceImp.getById(uid);
        data.put("code",200);
        if (user.getPassword().equals(oldpassword)){
            user.setPassword(newpassword);
            boolean check = serviceImp.updateById(user);
            if (check){
                data.put("msg","success");
            }else {
                data.put("msg","更新失败");
            }
        }else {
            data.put("msg","旧密码错误");
        }
        return data;
    }

    @PostMapping("/userLogin")
    public Map<String,Object> checkUser(@RequestParam (name = "name") String name,
                                        @RequestParam (name = "password") String password){
        Map<String,Object> data = new HashMap<>();
        User user = serviceImp.getByAccount(name);
        if (user!=null){
            if (user.getPassword().equals(password)) {
                data.put("code",200);
                data.put("data", user);
                data.put("msg", "success");
            }else {
                data.put("code",400);
                data.put("msg","密码错误");
            }
        }else {
            data.put("code",400);
            data.put("msg","账号错误");
        }
        return data;
    }
    @PostMapping("/getUserDataPage")
    public Map<String,Object> getUserDataPage(@RequestParam (name = "current") Integer current,
            @RequestParam(name = "size") Integer size){
        Map<String,Object> data = new HashMap<>();
        IPage<User> maps = serviceImp.selectAllPage(current,size);
        data.put("code",200);
        data.put("msg","success");
        data.put("data",maps);
        return data;
    }
    @PostMapping("/getUserByAccount")
    public Map<String, Object> getUserByAccount(@RequestParam(name = "name") String name){
        User user = serviceImp.getByName(name);
        Map<String,Object> data = new HashMap<>();
        if (user!=null){
            data.put("code",200);
            data.put("msg","查询成功");
            data.put("data",user);
        }else {
            data.put("code",400);
            data.put("msg","查询失败");
        }
        return data;
    }
    @PostMapping("/getUserById")
    public Map<String,Object> getUserById(@RequestParam String id){
        User user = serviceImp.getById(id);
        Map<String,Object> data = new HashMap<>();
        if (user != null){
            data.put("code",200);
            data.put("msg","查询成功");
            data.put("data",user);
            return data;
        }
        data.put("code",400);
        data.put("msg","未查询到数据");
        return data;
    }

    @DeleteMapping("/deleteById")
    public Map<String,Object> deleteById(@RequestParam String id){
        Map<String,Object> data = new HashMap<>();
        boolean check = serviceImp.removeById(id);
       // boolean check = serviceImp.deleteById(id);
        if (check){
            data.put("code",200);
            data.put("msg","删除成功");
            return data;
        }
            data.put("code",400);
            data.put("msg","删除失败");
            return data;
    }

    @PostMapping("/insert")
    public Map<String,Object> insertUserData(User user){
        boolean check = false;
        Map<String,Object> data = new HashMap<>();
        data.put("code",200);
        String username = user.getUsername();
        User user1 = serviceImp.getByAccount(username);
        if (user1==null){
                check = serviceImp.save(user);
            if (check){
                data.put("msg","新增成功");
            }else {
                data.put("msg","操作失败");
            }
        } else {
            data.put("msg","操作失败,账号已存在");
        }
        return data;

    }


    @PostMapping("/update")
    public Map<String,Object> updateById(User user){
        Map<String,Object> data = new HashMap<>();
        boolean check =  serviceImp.updateById(user);
        data.put("code",200);
        if (check ){
            data.put("msg","更新成功");
        }else {
            data.put("msg","更新失败");
        }
        return data;
    }

//    @PostMapping( "/getUserName")
//    public Map<String,Object> getUserName(Map<String,Object> maps){
//
//    }



}
